package com.wushijia.algorithm;

/**
 * 对称二叉树
 *
 * @author Administrator
 * @date 2021-06-28 14:59
 */
public class SymmetryBinaryTree {

  public static void main(String[] args) {
    ListNode root = new ListNode(1);
    ListNode r2 = new ListNode(2);
    ListNode r3 = new ListNode(2);
    ListNode r4 = new ListNode(4);
    ListNode r5 = new ListNode(4);
    root.left = r2;
    root.right = r3;
    r2.left = r4;
    r3.right = r5;
    System.out.println(new SymmetryBinaryTree().isSymmetric(root));
  }

  private boolean isSymmetric(ListNode root) {
    return root == null ? Boolean.TRUE : recur(root.left, root.right);
  }

  private boolean recur(ListNode left, ListNode right) {
    if (left == null && right == null) {
      return true;
    }
    if (left == null || right == null || left.val != right.val) {
      return false;
    }
    return recur(left.right, right.left) && recur(right.right, left.left);
  }
}
